{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.17746699  0.11989266]\n",
      " [ 0.1176311   0.13476761]\n",
      " [ 0.35493399  0.09530765]]\n"
     ]
    }
   ],
   "source": [
    "cpt = np.random.rand(3,2)\n",
    "# normalize\n",
    "cpt[0,0] = 1\n",
    "cpt[2,0] = 2\n",
    "const = np.sum(cpt)\n",
    "cpt = cpt/const\n",
    "print cpt\n",
    "x1 = [1,2]\n",
    "x2 = [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.65003208,  0.34996792])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tmpCpt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2. ...,  1.  1.  1.]\n",
      " [ 2.  3.  2. ...,  2.  3.  3.]]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:10: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future\n"
     ]
    }
   ],
   "source": [
    "N = 1000\n",
    "data = np.zeros((2, N))\n",
    "# apply gibbs sampling to generate the data\n",
    "x0 = [0,1]\n",
    "tmpCpt = np.sum(cpt, 0)\n",
    "for i in xrange(N):\n",
    "    # sample one from it\n",
    "    data[0,i] = int(np.random.choice(x1, 1, p=tmpCpt.tolist()))\n",
    "    # print tmpCpt.tolist()\n",
    "    tmpCpt2 = cpt[:,data[0,i]-1].tolist()\n",
    "    data[1,i] = int(np.random.choice(x2, 1,p=tmpCpt2 / np.sum(tmpCpt2)))\n",
    "    # print cpt[:,data[0,i]-1].tolist()\n",
    "p = 0.3 # percent of unknown data\n",
    "mask = np.random.rand(2, N)\n",
    "\n",
    "data[mask < p] = 0\n",
    "print data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "462"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(data[0,:] == 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.  1.  2.  1.  1.  1.  0.  0.  1.  2.  0.  0.  0.  1.  2.  0.  1.  2.\n",
      "  1.  0.  2.  2.  0.  2.  1.  2.  1.  0.  1.  2.  1.  0.  1.  0.  1.  1.\n",
      "  2.  2.  2.  1.  1.  2.  0.  1.  0.  1.  1.  0.  0.  0.  0.  0.  0.  0.\n",
      "  1.  1.  1.  0.  1.  1.  1.  0.  2.  1.  1.  1.  1.  0.  1.  0.  0.  1.\n",
      "  0.  0.  2.  0.  2.  0.  1.  1.  1.  2.  0.  1.  0.  0.  1.  1.  1.  1.\n",
      "  2.  2.  0.  2.  1.  1.  1.  0.  1.  1.  0.  0.  1.  1.  0.  1.  0.  0.\n",
      "  2.  1.  2.  0.  0.  1.  0.  2.  1.  1.  0.  0.  1.  1.  1.  2.  1.  0.\n",
      "  1.  2.  0.  2.  1.  1.  2.  1.  1.  1.  2.  1.  1.  0.  0.  1.  2.  1.\n",
      "  1.  1.  1.  1.  1.  2.  1.  2.  1.  2.  0.  1.  1.  1.  1.  0.  1.  1.\n",
      "  2.  1.  1.  1.  1.  1.  0.  1.  0.  2.  2.  1.  0.  1.  1.  0.  2.  2.\n",
      "  2.  1.  0.  2.  0.  1.  2.  0.  1.  2.  2.  1.  0.  0.  1.  2.  0.  0.\n",
      "  1.  0.  1.  2.  1.  1.  1.  1.  0.  0.  1.  0.  1.  1.  2.  1.  2.  1.\n",
      "  0.  0.  1.  1.  1.  1.  2.  0.  1.  1.  2.  1.  0.  1.  0.  1.  0.  0.\n",
      "  2.  1.  0.  0.  2.  1.  1.  1.  2.  1.  0.  2.  1.  1.  1.  2.  1.  1.\n",
      "  1.  1.  1.  1.  0.  1.  0.  2.  1.  2.  1.  0.  0.  0.  2.  1.  2.  1.\n",
      "  1.  2.  0.  0.  0.  2.  1.  0.  2.  0.  0.  0.  0.  1.  1.  0.  1.  2.\n",
      "  0.  0.  2.  2.  1.  1.  1.  0.  1.  1.  2.  2.  1.  0.  1.  1.  2.  2.\n",
      "  1.  1.  1.  2.  1.  0.  2.  1.  1.  1.  1.  0.  0.  1.  0.  1.  1.  2.\n",
      "  1.  0.  1.  1.  2.  2.  2.  2.  0.  1.  0.  2.  1.  1.  2.  1.  0.  1.\n",
      "  2.  0.  0.  1.  1.  2.  1.  0.  1.  1.  0.  1.  1.  1.  0.  1.  2.  1.\n",
      "  2.  1.  2.  2.  0.  0.  0.  1.  0.  1.  1.  1.  2.  1.  0.  1.  1.  2.\n",
      "  1.  2.  1.  1.  2.  0.  1.  2.  0.  0.  0.  2.  1.  0.  1.  1.  2.  0.\n",
      "  1.  0.  2.  0.  2.  2.  1.  2.  0.  1.  1.  1.  0.  2.  0.  2.  1.  0.\n",
      "  1.  0.  2.  1.  0.  1.  1.  2.  2.  0.  0.  0.  1.  0.  1.  2.  0.  1.\n",
      "  1.  0.  0.  1.  1.  1.  0.  1.  2.  2.  1.  1.  1.  1.  1.  1.  1.  1.\n",
      "  1.  0.  1.  2.  0.  2.  1.  2.  0.  2.  1.  1.  2.  1.  2.  2.  2.  0.\n",
      "  1.  2.  2.  1.  0.  2.  0.  1.  2.  1.  0.  0.  1.  0.  0.  0.  0.  1.\n",
      "  1.  2.  0.  1.  2.  0.  1.  1.  2.  1.  0.  1.  1.  1.  2.  2.  2.  2.\n",
      "  1.  0.  0.  0.  0.  1.  2.  1.  2.  2.  1.  0.  2.  2.  2.  0.  1.  2.\n",
      "  0.  1.  2.  0.  1.  2.  2.  0.  0.  1.  2.  0.  0.  1.  1.  0.  2.  1.\n",
      "  2.  2.  0.  1.  2.  2.  1.  1.  0.  2.  0.  1.  1.  1.  1.  1.  2.  1.\n",
      "  1.  0.  1.  0.  2.  0.  2.  1.  2.  0.  0.  1.  0.  2.  2.  1.  2.  1.\n",
      "  0.  0.  0.  2.  1.  0.  0.  1.  2.  0.  0.  2.  1.  2.  2.  1.  0.  1.\n",
      "  1.  1.  2.  2.  0.  2.  2.  2.  1.  0.  1.  2.  1.  0.  1.  2.  0.  0.\n",
      "  1.  1.  1.  1.  1.  0.  1.  2.  2.  0.  1.  2.  1.  1.  1.  0.  0.  0.\n",
      "  0.  1.  0.  1.  1.  0.  1.  1.  0.  2.  2.  1.  1.  1.  1.  0.  1.  2.\n",
      "  1.  2.  2.  0.  1.  0.  1.  1.  2.  2.  0.  2.  0.  1.  0.  1.  2.  0.\n",
      "  1.  1.  2.  2.  0.  1.  2.  2.  1.  0.  1.  1.  0.  2.  1.  1.  1.  0.\n",
      "  1.  1.  0.  1.  1.  1.  2.  2.  1.  1.  2.  1.  2.  2.  2.  1.  1.  1.\n",
      "  2.  1.  1.  1.  2.  2.  1.  1.  0.  1.  1.  1.  1.  0.  1.  1.  2.  1.\n",
      "  1.  1.  2.  0.  2.  0.  1.  2.  0.  1.  1.  2.  1.  1.  1.  1.  0.  2.\n",
      "  1.  0.  0.  1.  1.  0.  0.  1.  1.  2.  2.  2.  1.  1.  2.  1.  2.  1.\n",
      "  1.  1.  2.  0.  0.  0.  2.  2.  1.  1.  1.  1.  1.  2.  0.  2.  0.  1.\n",
      "  0.  1.  1.  0.  1.  1.  1.  0.  1.  0.  0.  0.  2.  1.  2.  1.  1.  2.\n",
      "  1.  1.  0.  1.  0.  1.  0.  0.  0.  1.  2.  2.  0.  1.  2.  0.  1.  0.\n",
      "  1.  2.  2.  1.  2.  1.  0.  0.  2.  1.  2.  0.  1.  2.  1.  1.  2.  0.\n",
      "  1.  1.  2.  0.  0.  1.  1.  2.  0.  1.  0.  2.  0.  0.  1.  0.  2.  2.\n",
      "  0.  1.  0.  0.  1.  1.  2.  2.  0.  0.  0.  2.  1.  1.  1.  1.  1.  1.\n",
      "  2.  0.  1.  1.  1.  1.  1.  1.  0.  1.  1.  1.  0.  0.  1.  1.  0.  1.\n",
      "  1.  0.  2.  1.  2.  2.  2.  0.  1.  1.  1.  1.  2.  1.  1.  1.  1.  1.\n",
      "  0.  1.  1.  1.  1.  0.  2.  1.  2.  2.  1.  1.  1.  0.  0.  0.  1.  1.\n",
      "  1.  1.  0.  2.  2.  1.  0.  2.  2.  0.  1.  2.  2.  2.  1.  0.  2.  1.\n",
      "  1.  0.  1.  1.  2.  2.  0.  0.  1.  1.  2.  2.  0.  1.  1.  1.  1.  0.\n",
      "  1.  1.  0.  1.  2.  0.  0.  2.  1.  0.  0.  2.  2.  0.  2.  0.  1.  0.\n",
      "  2.  2.  1.  1.  0.  1.  1.  0.  0.  2.  1.  1.  2.  0.  1.  2.  1.  1.\n",
      "  0.  1.  1.  0.  1.  2.  2.  1.  1.  1.]\n",
      "[ 2.  3.  2.  1.  1.  0.  3.  2.  0.  1.  3.  3.  3.  3.  1.  2.  0.  3.\n",
      "  3.  1.  3.  1.  2.  1.  3.  1.  1.  3.  0.  2.  0.  1.  0.  0.  2.  2.\n",
      "  1.  3.  0.  2.  3.  1.  1.  1.  3.  0.  1.  2.  3.  2.  3.  1.  1.  3.\n",
      "  0.  0.  3.  0.  3.  2.  0.  3.  2.  0.  3.  3.  1.  1.  2.  0.  0.  1.\n",
      "  0.  0.  0.  3.  1.  1.  0.  1.  0.  0.  3.  0.  0.  1.  1.  2.  3.  3.\n",
      "  3.  2.  3.  0.  1.  3.  2.  0.  2.  0.  0.  1.  0.  0.  0.  3.  3.  2.\n",
      "  2.  3.  1.  1.  0.  3.  0.  3.  3.  3.  1.  3.  3.  2.  1.  0.  1.  3.\n",
      "  1.  1.  0.  3.  1.  1.  1.  3.  1.  0.  0.  1.  1.  0.  0.  0.  0.  2.\n",
      "  3.  3.  3.  1.  3.  1.  0.  3.  0.  2.  1.  3.  3.  2.  0.  0.  3.  3.\n",
      "  3.  0.  1.  0.  3.  2.  0.  1.  2.  2.  1.  2.  3.  1.  1.  3.  2.  1.\n",
      "  1.  3.  2.  3.  3.  1.  0.  3.  0.  3.  2.  3.  2.  2.  3.  2.  0.  3.\n",
      "  3.  1.  1.  0.  2.  3.  3.  3.  2.  3.  2.  2.  3.  3.  0.  3.  1.  0.\n",
      "  0.  3.  0.  3.  2.  3.  2.  1.  3.  3.  2.  3.  3.  3.  3.  2.  3.  0.\n",
      "  1.  1.  2.  3.  1.  3.  3.  3.  2.  2.  3.  2.  3.  0.  2.  3.  3.  2.\n",
      "  0.  0.  3.  1.  1.  1.  1.  1.  3.  3.  3.  1.  0.  3.  1.  0.  1.  0.\n",
      "  2.  0.  2.  3.  3.  0.  1.  0.  2.  0.  3.  0.  0.  0.  3.  2.  1.  0.\n",
      "  0.  0.  0.  0.  2.  0.  1.  1.  3.  3.  1.  0.  0.  1.  0.  3.  0.  2.\n",
      "  1.  0.  0.  3.  3.  1.  2.  2.  3.  3.  1.  1.  3.  0.  1.  0.  3.  2.\n",
      "  3.  2.  3.  0.  0.  0.  1.  1.  3.  0.  3.  2.  1.  1.  1.  1.  3.  1.\n",
      "  1.  3.  2.  1.  1.  0.  3.  2.  3.  0.  3.  1.  3.  3.  2.  3.  1.  0.\n",
      "  0.  2.  3.  0.  3.  1.  3.  2.  2.  1.  0.  3.  1.  2.  0.  0.  1.  0.\n",
      "  3.  0.  0.  0.  1.  0.  0.  2.  0.  2.  3.  2.  3.  1.  3.  3.  1.  0.\n",
      "  2.  2.  0.  0.  3.  0.  1.  2.  1.  0.  1.  3.  3.  3.  2.  3.  1.  0.\n",
      "  3.  2.  0.  1.  1.  0.  0.  2.  0.  2.  3.  1.  3.  3.  0.  1.  3.  0.\n",
      "  3.  1.  3.  3.  0.  2.  2.  1.  1.  1.  0.  3.  3.  3.  0.  3.  3.  0.\n",
      "  0.  1.  3.  0.  0.  3.  3.  0.  2.  1.  0.  2.  2.  0.  2.  1.  0.  1.\n",
      "  1.  0.  3.  0.  0.  1.  3.  0.  0.  3.  0.  1.  3.  0.  3.  3.  1.  3.\n",
      "  3.  0.  2.  1.  0.  2.  1.  0.  2.  1.  0.  3.  2.  0.  3.  1.  1.  1.\n",
      "  2.  1.  3.  1.  2.  0.  3.  3.  0.  1.  1.  1.  0.  2.  0.  1.  0.  1.\n",
      "  2.  1.  2.  0.  3.  2.  3.  0.  0.  0.  2.  0.  2.  0.  3.  2.  2.  3.\n",
      "  1.  2.  0.  0.  3.  3.  3.  3.  0.  0.  2.  3.  3.  3.  0.  3.  2.  0.\n",
      "  0.  3.  2.  1.  2.  3.  1.  3.  3.  2.  1.  3.  0.  2.  2.  1.  3.  0.\n",
      "  0.  0.  2.  0.  0.  3.  2.  3.  0.  0.  2.  1.  0.  0.  0.  0.  1.  3.\n",
      "  1.  0.  1.  3.  3.  1.  3.  0.  1.  2.  3.  2.  3.  0.  2.  2.  3.  0.\n",
      "  2.  3.  0.  0.  3.  0.  3.  3.  3.  0.  0.  0.  2.  0.  3.  0.  3.  3.\n",
      "  0.  1.  0.  0.  1.  1.  0.  2.  2.  1.  0.  1.  1.  1.  0.  1.  3.  2.\n",
      "  2.  3.  3.  0.  3.  1.  3.  3.  0.  2.  1.  0.  0.  0.  2.  1.  2.  1.\n",
      "  3.  0.  2.  1.  0.  0.  1.  1.  2.  0.  1.  3.  3.  1.  3.  3.  3.  3.\n",
      "  3.  1.  3.  3.  3.  3.  2.  0.  0.  3.  1.  1.  3.  0.  3.  1.  2.  3.\n",
      "  0.  0.  2.  1.  2.  2.  0.  3.  0.  2.  0.  3.  0.  0.  3.  0.  0.  1.\n",
      "  3.  3.  3.  3.  2.  0.  0.  2.  1.  0.  3.  1.  1.  3.  0.  0.  3.  3.\n",
      "  0.  2.  3.  3.  3.  0.  3.  1.  2.  0.  0.  1.  2.  3.  2.  0.  0.  0.\n",
      "  3.  0.  2.  2.  1.  1.  0.  1.  0.  0.  3.  3.  1.  3.  1.  1.  3.  0.\n",
      "  0.  1.  0.  3.  3.  3.  3.  0.  0.  0.  3.  2.  0.  0.  2.  3.  1.  1.\n",
      "  0.  0.  1.  0.  3.  0.  0.  0.  3.  2.  3.  1.  0.  3.  2.  3.  3.  0.\n",
      "  2.  0.  0.  0.  1.  2.  2.  3.  2.  2.  3.  1.  3.  0.  0.  0.  2.  1.\n",
      "  1.  0.  3.  1.  3.  3.  3.  2.  1.  1.  3.  3.  0.  3.  0.  0.  3.  2.\n",
      "  1.  1.  1.  2.  1.  3.  0.  1.  0.  1.  0.  1.  0.  3.  1.  0.  2.  3.\n",
      "  0.  3.  2.  1.  3.  3.  1.  0.  2.  1.  0.  0.  3.  3.  1.  1.  0.  3.\n",
      "  3.  3.  1.  3.  2.  3.  1.  0.  0.  1.  3.  1.  2.  1.  3.  1.  2.  0.\n",
      "  3.  0.  2.  0.  3.  1.  0.  0.  0.  0.  0.  1.  3.  0.  0.  0.  1.  3.\n",
      "  3.  3.  0.  2.  1.  0.  2.  0.  2.  3.  0.  1.  2.  0.  3.  3.  3.  3.\n",
      "  2.  3.  1.  1.  3.  2.  1.  0.  0.  3.  3.  3.  0.  3.  3.  3.  2.  2.\n",
      "  0.  0.  1.  2.  1.  0.  2.  0.  3.  3.  2.  2.  2.  2.  0.  3.  0.  0.\n",
      "  2.  3.  3.  0.  2.  3.  3.  3.  2.  2.  0.  3.  3.  1.  2.  1.  3.  3.\n",
      "  0.  3.  1.  0.  3.  3.  0.  2.  3.  3.]\n"
     ]
    }
   ],
   "source": [
    "with open (\"data.txt\",\"w\")as fp:\n",
    "    for line in data:\n",
    "        print line\n",
    "        for i in xrange(len(line)):\n",
    "            if i != len(line) - 1:\n",
    "                fp.write(str(int(line[i])) + \", \")\n",
    "            else:\n",
    "                fp.write(str(int(line[i])))\n",
    "        fp.write(\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "tmp = np.array([ 199.48,120.06,338.06,120.88,131.51,95.860])\n",
    "tmp = tmp / np.sum(tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:3: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future\n",
      "  app.launch_new_instance()\n"
     ]
    }
   ],
   "source": [
    "avgCpt = np.zeros((3, 2))\n",
    "for i in xrange(N):\n",
    "    avgCpt[data[1,i]-1, data[0,i]-1] += 1\n",
    "avgCpt = avgCpt/np.sum(avgCpt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.19831983  0.11936173  0.33609385  0.12017696  0.13074514  0.09530248]\n",
      "[[ 0.17746699  0.11989266]\n",
      " [ 0.1176311   0.13476761]\n",
      " [ 0.35493399  0.09530765]]\n",
      "[[ 0.092  0.127]\n",
      " [ 0.056  0.12 ]\n",
      " [ 0.314  0.291]]\n",
      "[ 0.65003208  0.34996792]\n"
     ]
    }
   ],
   "source": [
    "print tmp\n",
    "print cpt\n",
    "print avgCpt\n",
    "print tmpCpt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:2: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future\n",
      "  from ipykernel import kernelapp as app\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0.0033381790418454296, 0.18905551089099382, 0.048171997949225116]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = 1\n",
    "cpt[:,data[0,i]-1].tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.18033236  0.10003034  0.35495207  0.1324742   0.15316776  0.07904327]\n",
      "[ 0.19831983  0.11936173  0.33609385  0.12017696  0.13074514  0.09530248]\n",
      "here is the ground truth:\n",
      "[ 0.17746699  0.117631    0.35493399  0.11989266  0.13476761  0.09530765]\n"
     ]
    }
   ],
   "source": [
    "new = np.array([183.09,101.56,360.38,134.50,155.51,80.252])\n",
    "new = new / np.sum(new)\n",
    "tmp = np.array([ 199.48,120.06,338.06,120.88,131.51,95.860])\n",
    "tmp = tmp / np.sum(tmp)\n",
    "cpt_row = np.array([0.17746699,0.117631,0.35493399,0.11989266,0.13476761,0.09530765])\n",
    "print new\n",
    "print tmp\n",
    "print \"here is the ground truth:\"\n",
    "print cpt_row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
